package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.servicenode;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.ServiceNodeLevelEnum;
import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.ServiceNodeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.ServiceNode;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Ztree;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.servicenode.JsmServiceNodeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian@cqhilink.com on 2016/11/17.
 * 业务节点配置-服务层实现
 */

public class JsmServiceNodeServiceImpl implements JsmServiceNodeService {

    // 业务节点搜索
    @Autowired
    private ServiceNodeMapper serviceNodeMapper;

    /**
     * 业务体系划分新增或者修改
     *
     * @param serviceNode 业务节点 - 新增 或者 修改
     */
    @Override
    public void insertServiceNode(ServiceNode serviceNode,Employee employee) throws Exception {
        String userId = employee.getEmployeeId();
        if (serviceNode.getNodeId() == null) {
            serviceNode.setNodeId(UniqueUtil.uuid());
            serviceNode.setStatus(BigDecimal.valueOf(0));
            serviceNode.setCreateTime(new Date());
            serviceNode.setCreatorId(userId);
            /* 新增 */
            serviceNodeMapper.insertSelective(serviceNode);
        } else {
            serviceNode.setModifyTime(new Date());
            serviceNode.setModifierId(userId);
            /* 修改 */
            serviceNode.setStatus(BigDecimal.valueOf(0));
            serviceNodeMapper.updateByPrimaryKeySelective(serviceNode);
        }


    }

    /**
     * 获取业务节点-树
     *
     * @return
     */
    @Override
    public List<Ztree> getInitZtree(String nodeNames) {
        ServiceNode node = new ServiceNode();
        node.setNodeNames(nodeNames);
        List<ServiceNode> list = serviceNodeMapper.getInitZtree(node);
        List<Ztree> ztreeList = offsetZtree(list);
        return ztreeList;
    }

    /**
     * @param serviceNodeId 查询业务节点
     */
    @Override
    public ServiceNode queryServiceNode(String serviceNodeId) {
        ServiceNode serviceNode = serviceNodeMapper.selectByPrimaryKey(serviceNodeId);
        return serviceNode;
    }

    /**
     * @param nodeId 业务节点-删除 （逻辑删除）
     */
    @Override
    public Integer delServiceNode(String nodeId) throws Exception {
        return serviceNodeMapper.deleteByPrimaryKey(nodeId);
    }

    /**
     * 根据ServiceNode实体类集合的nodeId查询parentNodeId数据集合
     *
     * @param list
     * @return List<ServiceNode>
     * @Author 卢曼成
     * @CreateDateTime 2017/3/24 14:51
     */
    @Override
    public List<ServiceNode> selectByParentList(List<ServiceNode> list) {
        return serviceNodeMapper.selectByParentList(list);
    }

    /**
     * 查询2级业务体系树形
     *
     * @return List<Ztree>
     * @Author 卢曼成
     * @CreateDateTime 2017/3/24 14:51
     */
    @Override
    public List<Ztree> initStationZtree() {
        ServiceNode sn = new ServiceNode();
        sn.setNodeId("#");
        List<ServiceNode> snlist = new ArrayList<ServiceNode>();
        snlist.add(sn);
        List<ServiceNode> aList = serviceNodeMapper.selectByParentList(snlist);
        List<Ztree> list = new ArrayList<Ztree>();
        if (aList != null && aList.size() > 0) {
            List<ServiceNode> bList = serviceNodeMapper.selectByParentList(aList);
            list.addAll(offsetZtree(aList));
            list.addAll(offsetZtree(bList));
        }
        return list;
    }

    /**
     * 只查询业务节点的业务体系
     */
    @Override
    public List<Ztree> getServieNodeZtree() {
        List<ServiceNode> serviceNodes = new ArrayList<>();
        ServiceNode serviceNode = new ServiceNode();
        serviceNode.setNodeLevel("1");
        serviceNodes = serviceNodeMapper.getInitZtree(serviceNode);
        List<Ztree> ztreeList = new ArrayList<>();
        ztreeList = offsetZtree(serviceNodes);
        return ztreeList ;
    }

    /**
     * @param serviceNodes
     * @return 将业务节点数据进行转换成树的数据
     */
    private List<Ztree> offsetZtree(List<ServiceNode> serviceNodes) {
        List<Ztree> ztreeList = new ArrayList<>();
        if (serviceNodes != null) {
            /* 将业务节点转换成树 */
            for (ServiceNode node : serviceNodes) {
                Ztree ztree = new Ztree();
                ztree.setId(node.getNodeId());
                ztree.setpId(node.getParentNodeId());
                ztree.setName(node.getNodeNames());
                ztree.setOrgLevel(node.getNodeLevel());
                //转换使用树形显示，生产单元和业务节点内容
                if (Integer.valueOf(node.getNodeLevel()) == ServiceNodeLevelEnum.ServiceNodeLevel_NUM1.getCode()) {
                    ztree.setIconSkin(TreeIconSkinEnum.ICON_SERVICE_NODE.getDesc());
                } else {
                    ztree.setIconSkin(TreeIconSkinEnum.ICON_PRODUCTION_UNIT.getDesc());
                }
                if (node.getParentNodeId().equals("#")) ztree.setIconSkin(TreeIconSkinEnum.ICON_BUIDING.getDesc());
                ztreeList.add(ztree);
            }
        }
        return ztreeList;
    }

}
